<% if request.fullpath.include? "?"
     given_params = request.fullpath.split(/\?/)[1]
     clean_fullpath = locations_path + "?" + given_params.strip.split(/&/).collect{ |p| p =~/^center\_(lat|lng)/ ? nil : p }.compact.join("&") + "&"
   else
     clean_fullpath = locations_path + "?"
   end
%>

<%= render 'searchbar' %>

<div id="mainmap_container"><div id="map"></div></div>

<% content_for :page_scripts do %>
<script src="//maps.google.com/maps/api/js?v=3.8&amp;sensor=false&amp;key=&amp;libraries=geometry&amp;language=&amp;hl=&amp;region=" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">

  // ================= functions =================

  function ControlSpacer(spacerDiv, map) {
    spacerDiv.style.height = '3em';
  }
    
  function initialize() {
    var ini_zoom = 4;
    // Kearney, Nebraska :P
    var ini_lat = 40.713956;
    var ini_lng = -99.09668;
    var ini_type = google.maps.MapTypeId.ROADMAP;
    <% unless @perma.nil? %>
      ini_lat = <%= @perma[:lat] %>;
      ini_lng = <%= @perma[:lng] %>;
      ini_zoom = <%= @perma[:zoom] %>;
      ini_type = '<%= @perma[:type] %>';
    <% end %>
    var mapOptions = {
      zoom: ini_zoom,
      center: new google.maps.LatLng(ini_lat,ini_lng),
      mapTypeId: ini_type,
    };
    map = new google.maps.Map(document.getElementById('map'),mapOptions);
    geocoder = new google.maps.Geocoder();

    pb = progressBar();
    map.controls[google.maps.ControlPosition.RIGHT].push(pb.getDiv());

    google.maps.event.addListener(map, 'maptypeid_changed', function(event) {
      update_permalink();
    });

    google.maps.event.addListener(map, 'rightclick', function(event) {
      remove_add_marker();
      place_add_marker(event.latLng);
    });

    google.maps.event.addListener(map, 'click', function(event) {
      if(openMarker != null && openInfoWindow != null){
        openInfoWindow.close();
        openMarker = null;
        openInfoWindow = null;
      }
    });
    
    google.maps.event.addListenerOnce(map, 'tilesloaded', function(event) {
      update_display(true,ini_zoom);
    });
             
    google.maps.event.addListener(map, 'bounds_changed', function() {
      google.maps.event.addListenerOnce(map, 'idle', function(){
        update_display(false);
      });
    });
    google.maps.event.addListener(map, 'zoom_changed', function() {
      google.maps.event.addListenerOnce(map, 'idle', function(){
        update_display(false);
      });
    });
    map.setTilt(0);    
  }

  // ================== main-level code ===================

  google.maps.event.addDomListener(window, 'load', initialize);
  
  // hide the 'zoom to me' button if browser isn't supporting geolocation
  if(!navigator.geolocation){
    $('recenter_button').hide();
  }
  navigator.geolocation.getCurrentPosition(function(position){},function(error){
    $('recenter_button').hide();
  });
</script>
<%= javascript_include_tag "maplabel" %>
<%= javascript_include_tag "progressBar" %>
<%= javascript_include_tag "richmarker" %>
<%= javascript_include_tag "mapcommon" %>
<%= javascript_include_tag "mainmap" %>
<% end %>
